import { getUserByPage, getUserBasicInfoById } from '@/api/user/user';
import { merge } from 'lodash';
import pageMixins from '@/store/mixins/page.js';
import { Message } from 'element-ui';

export default merge(
  {
    namespaced: true,
    state: {
      query: {
        name: '',
        status: []
      },
      list: [],
      isLoading: false
    },
    getters: {
      g_query(state) {
        return state.query;
      },
      g_name(state) {
        return state.query.name;
      },
      g_status(state) {
        return state.query.status;
      }
    },
    mutations: {
      s_qQuery(state, query) {
        state.query = {
          ...state.query,
          query
        };
      },
      s_qname(state, name) {
        state.query.name = name;
      },
      s_qStatus(state, status) {
        state.query.status = status || [];
      }
    },
    actions: {
      /**
       * @description 登录
       * @param {Object} param vm {Object} vue 实例
       * @param {Object} param username {String} 用户账号
       * @param {Object} param password {String} 密码
       * @param {Object} param route {Object} 登录成功后定向的路由对象
       */
      fnGetUserByPage({ commit, state }) {
       
        let params = {
          page: state.page
        };

        return new Promise((resolve, reject) => {
          getUserByPage(params)
            .then(resp => {
             
              state.loading = false;
              if (resp.respCode !== 0) {
                reject(`${resp.respMsg}`);
                return;
              }

              let { result } = resp;
              state.page = {
                ...state.page,
                ...result.page
              };
              state.list = result.list || [];
              resolve(resp);
            })
            .catch(error => {
              state.loading = false;
              Message({
                type: 'error',
                message: '加载数据失败.'
              });
            });
        });
      },
      reloadActivityByPage({ commit, dispatch, state }, { currentPage }) {
        if (currentPage) {
          state.page.currentPage = currentPage;
        } else {
          state.page.currentPage = 1;
        }

        return new Promise(async (resolve, reject) => {
          await dispatch('fnGetUserByPage');
        });
      },

      getUserBasicInfo({ commit, state }, id) {
       

        return new Promise((resolve, reject) => {
          getUserBasicInfoById(id)
            .then(resp => {
             
              state.loading = false;
              if (resp.respCode !== 0) {
                reject(`${resp.respMsg}`);
                return;
              }

             
              resolve(resp.result);
            })
            .catch(error => {
              state.loading = false;
              Message({
                type: 'error',
                message: '加载数据失败.'
              });
            });
        });
      }
    }
  },
  pageMixins
);
